<?php
class order_hook {
	public function create_order() {
		// echo '下单成功钩子';
	}

	public function pay_success($ret) {
		// echo '钩子：支付成功';
		if($ret['result'] == 'success') {
			if($ret['out_trade_no'][0] != 'm') {
				model('order/order_sub', 'service')->set_order($ret['out_trade_no'], 'pay', 0, array(
					'pay_method' => $ret['pay_code'],
					'pay_sn' => $ret['trade_no'],
				));
				showmessage('订单支付成功', '../../index.php?m=order&c=order&a=pay_success&order_sn='.$ret['out_trade_no']);
			}
		}
	}

	public function confirm_order() {
		// echo '钩子：卖家确认订单';
	}

	public function skus_delivery() {
		// echo '钩子：订单商品已发货';
	}

	public function delivery_finish() {
		// echo '钩子：确认收货(完成)';
	}

	// 钩子：整个订单完成
	public function order_finish($order_sn) {
		$order = model('order/order')->where(array('sn' => $order_sn))->find();
		/* 增加会员经验值 */
		// 获取后台经验获取比例配置
		$exp_rate = (float) model('admin/setting','service')->get_setting('exp_rate');
		if ($order && $exp_rate) {
			$exps = sprintf('%.2f', $order['paid_amount'] * $exp_rate);
			model('member/member','service')->action_exp($order['buyer_id'] ,$exps);
		}
		/* 扣除订单冻结余额 */
		model('member/member','service')->action_frozen($order['buyer_id'],$order['paid_amount'],false,'完成订单，扣除冻结中的余额');
		/* 增加skus销量 */
		$skus = model('order/order_sku','service')->get_by_order_sn($order_sn);
		if ($skus) {
			foreach ($skus as $sku) {
				model('goods/goods_index')->where(array('sku_id' => $sku['sku_id']))->setInc('sales',$sku['buy_nums']);
			}
		}
	}
}